#include "comm/my_pool.h"
#include "comm/my_mem.h"

void test_pool_mem_alloc(my_pool_t *pool)
{
    void *p;
    p = my_pool_malloc(pool, 1);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 100);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 900);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 900);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 900);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 900);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 328);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 900);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 900);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 900);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 1024);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 1024);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 4096);
    my_pool_free(pool, p);
    p = my_pool_malloc(pool, 8192);
    // release in my_pool_destroy
    // my_pool_free(pool, p);
}

static inline void
my_test_cleanup(void *data)
{
    DD("cleanup %d", (int)(intptr_t)data);
}

void
test_pool_cleanup(my_pool_t *pool) 
{
    my_cleanup_item_t *item1 = my_pool_add_cleanup(pool, my_test_cleanup, (void *)1);
    my_cleanup_item_t *item2 = my_pool_add_cleanup(pool, my_test_cleanup, (void *)2);
    my_cleanup_item_t *item3 = my_pool_add_cleanup(pool, my_test_cleanup, (void *)3);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)4);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)5);
    my_pool_del_cleanup(pool, item1);
    my_pool_del_cleanup(pool, item2);
    my_pool_del_cleanup(pool, item3);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)6);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)7);
    my_pool_add_cleanup(pool, my_test_cleanup, (void *)8);
}

int main() {
    my_pool_t *pool = my_pool_create();

    test_pool_mem_alloc(pool);

    test_pool_cleanup(pool);

    my_pool_destroy(pool);

    return 0;
}